package com.yaolifei.flower.core.impl;

import java.util.Collection;

import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Entity;
import org.nutz.dao.entity.MappingField;
import org.nutz.dao.entity.PkType;
import org.nutz.dao.entity.annotation.ColType;
import org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert;
import org.nutz.dao.jdbc.JdbcExpertConfigFile;

import com.yaolifei.flower.core.CustomJdbcExpert;

public class CustomMySqlJdbcExpert extends MysqlJdbcExpert implements
		CustomJdbcExpert {

	protected Dao dao;

	public CustomMySqlJdbcExpert(JdbcExpertConfigFile conf) {
		super(conf);
	}

	@Override
	public boolean addColumn(MappingField... fields) {
		if (fields.length == 0) {
			return false;
		}
		StringBuilder sb = new StringBuilder();
		Entity<?> en = fields[0].getEntity();
		for (int i = 0; i < fields.length; i++) {
			MappingField mf = fields[i];
			sb.append("ALTER TABLE " + en.getTableName() + " ADD ");
			sb.append(mf.getColumnName());

			sb.append(' ').append(evalFieldType(mf));
			// 非主键的 @Name，应该加入唯一性约束
			if (mf.isName() && en.getPkType() != PkType.NAME) {
				sb.append(" UNIQUE NOT NULL");
			}
			// 普通字段
			else {
				// 下面的关于Timestamp处理，是因为MySql中第一出现Timestamp的话，如果没有设定default，数据库默认会设置为CURRENT_TIMESTAMP
				if (mf.isUnsigned())
					sb.append(" UNSIGNED");

				if (mf.isNotNull()) {
					sb.append(" NOT NULL");
				} else if (mf.getColumnType() == ColType.TIMESTAMP) {
					sb.append(" NULL");
				}

				if (mf.isAutoIncreasement())
					sb.append(" AUTO_INCREMENT");

				if (mf.getColumnType() == ColType.TIMESTAMP) {
					if (mf.hasDefaultValue()) {
						sb.append(" ").append(getDefaultValue(mf));
					} else {
						if (mf.isNotNull()) {
							sb.append(" DEFAULT 0");
						} else {
							sb.append(" DEFAULT NULL");
						}
					}
				} else {
					if (mf.hasDefaultValue())
						sb.append(" DEFAULT '").append(getDefaultValue(mf))
								.append("'");
				}
			}

			if (mf.hasColumnComment()) {
				sb.append(" COMMENT '").append(mf.getColumnComment())
						.append("'");
			}
			sb.append(";");
		}
		// 执行创建语句
		dao.execute(Sqls.create(sb.toString()));

		return true;
	}

	@Override
	public boolean addColumns(Collection<MappingField> mf) {
		MappingField[] fields = mf.toArray(new MappingField[mf.size()]);
		return addColumn(fields);
	}

	@Override
	public boolean dropColumn(MappingField mf) {
		Entity<?> en = mf.getEntity();
		StringBuilder sb = new StringBuilder("ALTER TABLE " + en.getTableName()
				+ " DROP COLUMN " + mf.getColumnName());
		dao.execute(Sqls.create(sb.toString()));
		return true;
	}

	@Override
	public boolean renameColumn(MappingField mf, String newName) {
		Entity<?> en = mf.getEntity();
		StringBuilder sb = new StringBuilder("ALTER TABLE " + en.getTableName() + " CHANGE ");
		sb.append(mf.getColumnName()).append(" ").append(newName).append(" ");
		sb.append(evalFieldType(mf));
		dao.execute(Sqls.create(sb.toString()));
		return true;
	}

	@Override
	public boolean changeColumnType(String column, ColType type) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean renameTable(String originName, String newName) {
		// TODO Auto-generated method stub
		return false;
	}

}
